#include "DBCHandler.h"
#include "WorldHandler.h"
#include <sstream>

DBCHandler globalDBCHandler;

DBCHandler::DBCHandler()
{

}

DBCHandler::~DBCHandler()
{

}

bool DBCHandler::loadSingleDBC(std::string filename, std::map<unsigned int, std::string[DBC_FIELD_COUNT]>* entriesMap)
{
	std::ifstream inFile(filename.c_str(), std::ios::in);
	if(inFile.fail())
	{
		return false;
	}

	logMessage("Loading " + filename + ".");
	std::string line;
	while(!inFile.eof())
	{
		getline(inFile, line);
		if('#' == line.at(0))
		{
			continue;
		}
	
		std::string props[DBC_FIELD_COUNT];
		size_t offset = 0;
		unsigned int i;
		for(i = 0; i < DBC_FIELD_COUNT; ++i)
		{
			offset = line.find('|', 0);
			if(offset == std::string::npos)
			{
				break;
			}
		
			std::string curProp = line.substr(0, offset);
			props[i] = curProp;

			std::cout << "snasom [" << i << "]" + props[i] + "\n";
			line = line.substr(offset+1);
		}

		if(i == DBC_FIELD_COUNT
		|| NULL == entriesMap) // valid item
		{
			std::stringstream ss;
			
			if(filename == "zones.dbc")
			{
				ZoneObject* zoneObject = new ZoneObject;

				zoneObject->setName(props[ZONE_NAME]);
				zoneObject->setTextureName(props[ZONE_TEXTURENAME]);
				
				globalWorldHandler.addZoneObject(zoneObject);
			}
			else if(filename == "enemies.dbc")
			{			
				unsigned int zoneMapKey;
				ss << props[ENEMY_ZONEID];
				ss >> zoneMapKey;
				ZoneObject* zoneObject = globalWorldHandler.getZoneObject(zoneMapKey);
				zoneObject->addEnemyKey(zoneMapKey);
			}

			if(entriesMap != NULL)
			{
				unsigned int mapKey;
				ss << props[PROP_ID];
				ss >> mapKey;
				entriesMap->insert(std::pair<unsigned int, std::string[DBC_FIELD_COUNT]>(mapKey, props));
			}
		}
		else
		{
			std::cout << "Invalid item\n";
		}
	}

	inFile.close();
	
	return true;
}

void DBCHandler::loadAllDBCs()
{
	//loadSingleDBC("items.dbc", &itemsMap);
	loadSingleDBC("zones.dbc", NULL);
	loadSingleDBC("enemies.dbc", &enemiesMap);
	//std::string enemyProps[DBC_FIELD_COUNT];
	//enemyProps[ENEMY_NAME] = "Skeleton Mage";
	//enemiesMap.insert(std::pair<unsigned int, std::string[DBC_FIELD_COUNT]>(0, enemyProps));
}

EnemyObject* DBCHandler::createEnemy(unsigned int mapKey, core::vector3df position, int ID)
{
	//TODO: put the logic up until logic is created in same place for LivingObject and ZoneObject
	std::string enemyProps[DBC_FIELD_COUNT];
	std::map<unsigned int, std::string[DBC_FIELD_COUNT]>::iterator it = enemiesMap.find(mapKey);
	
	for(unsigned int i = 0; i < DBC_FIELD_COUNT; ++i)
	{
		enemyProps[i] = it->second[i]; 
	}
	std::cout << "mapKey:" << mapKey << "\n";
	
	EnemyObject* enemy = new EnemyObject;
	enemy->setName(enemyProps[ENEMY_NAME]);
	enemy->setModelName(enemyProps[ENEMY_MODELNAME]);
	enemy->setTextureName(enemyProps[ENEMY_TEXTURENAME]);
	enemy->setIsMoving(false);
	enemy->setStartMoving(false);
	enemy->setSpeedConstant(100.f);
	enemy->setPosition(position);
	enemy->setEnemyMapKey(mapKey);
	enemy->setMovementInfo(position, core::vector3df(0, 0, 0), core::vector3df(0, 0, 0), 0.f, 0.f);
	enemy->setIsInTown(false);
	
	globalWorldHandler.addWorldObject(enemy, ID);
	
	return enemy;
}

// TODO: createDoodad
